package cn.dm.ServiceImpl;

import cn.dm.Service.UserLoginService;
import cn.dm.client.RestDmImageClient;
import cn.dm.client.RestDmUserClient;
import cn.dm.pojo.DmImage;
import cn.dm.pojo.DmUser;
import com.alibaba.fastjson.JSONObject;
import common.Constants;
import common.EmptyUtils;
import common.MD5;
import common.RedisUtils;
import dto.Dto;
import dto.DtoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import vo.DmUserVO;
import vo.TokenVO;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@Component
public class UserLoginServiceImpl implements UserLoginService {

    @Resource
    private RestDmUserClient restDmUserClient;
    @Resource
    private RestDmImageClient restDmImageClient;
    @Resource
    private RedisUtils redisUtils;

    @Override
    public Dto Login(String phone, String password) throws Exception {
        Map<String,Object> param=new HashMap<String, Object>();
        param.put("phone",phone);
        param.put("password",password);
        //获取用户对象集合
        List<DmUser> dmUserList= restDmUserClient.getDmUserListByMap(param);
        if(EmptyUtils.isEmpty(dmUserList)){
            return DtoUtil.returnFail("账户名或密码错误","1006");
        }else {
            //获取第一个用户
            DmUser dmUser = dmUserList.get(0);
            String userJson=(String) redisUtils.get(Constants.USER_TOKEN_PREFIX+dmUser.getId());
            if(EmptyUtils.isEmpty(userJson)) {
                Map<String, Object> imgParam = new HashMap<>();
                imgParam.put("targetId", dmUser.getId());
                imgParam.put(("category"), Constants.Image.ImageCategory.user);
                DmUserVO dmUserVO = new DmUserVO();
                //将用户对象复制到Vo中
                BeanUtils.copyProperties(dmUser, dmUserVO);
                //获取用户头像
                DmImage dmImage = restDmImageClient.getDmImageListByMap(imgParam).get(0);
                //封装对象
                dmUserVO.setUserId(dmUser.getId());
                dmUserVO.setImageId(dmImage.getId());//获取图片的类型id
                dmUserVO.setImgUrl(dmImage.getImgUrl());//获取图片路径
                //生成登陆token
                TokenVO tokenVO=createToken(dmUser);
                Object[] dtoObject={dmUserVO,tokenVO};
                return DtoUtil.returnDataSuccess(dmUserVO);
                //将token保存redis
                String UserVOJSON= JSONObject.toJSONString(dmUserVO);
                redisUtils.set(dmUser.getId().toString(),Constants.Redis_Expire.SESSION_TIMEOUT,UserVOJSON);
                redisUtils.set(Constants.USER_TOKEN_PREFIX+dmUser.getId(),
                        Constants.Redis_Expire.SESSION_TIMEOUT,tokenVO.getToken());
                return DtoUtil.returnDataSuccess(dtoObject);
            }else {
                return DtoUtil.returnFail("用户已登陆","0001");
            }
        }
    }



    private TokenVO createToken (DmUser dmUser){
        String token="DM-"+ MD5.getMd5(dmUser.getPhone()+dmUser.getId(),16);
        TokenVO tokenVO=new TokenVO();
        tokenVO.setToken(token);
        tokenVO.setExpTime(Constants.Redis_Expire.SESSION_TIMEOUT);
        tokenVO.setGenTime(new Date().getTime());
        return null;
    }

}
